package com.mcxx.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.UnifiedStorage;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.core.utils.ResponseExportUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;



@Api("文件上传下载")
@RestController
@RequestMapping("file/v2")
public class FileController extends DefaultRestService {
    private final static Logger log = LoggerFactory.getLogger(FileController.class);
    @Autowired
    private UnifiedStorage unifiedStorage;
    @Autowired
    private FileService fileService;
    private static String unixPath="/opt/download/";
    private static String winPath = "E:";


    @PostMapping("testUpload")
    @ApiOperation(value="测试图片上传",notes="图片上传")
    public void testUpload(String filePath) throws IOException {
        String objectId = unifiedStorage.testUpload();
        unifiedStorage.testDownload(objectId);
    }


    @PostMapping("upload")
    @ApiOperation(value="图片上传",notes="图片上传")
    public void upload(@RequestParam("file") MultipartFile file) throws IOException {
        if(file.isEmpty()){
            throw new BizException("上传的文件是空的");
        }
        String imgKey = fileService.upload(file.getInputStream(), file.getOriginalFilename());
        String imgUrl = fileService.getFileUrl(imgKey);

        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("imgKey", imgKey);
        resultMap.put("imgUrl", imgUrl);

        setData(resultMap);
    }

    @RequestMapping(value = "download",method = RequestMethod.POST)
    @ApiOperation(value="下载文件",notes="下载文件")
    public void downloadURL(@RequestBody String json, HttpServletResponse response) {
        try {
            if(StringUtils.isNotBlank(json)){
                JSONObject jsonObject =  JSONObject.parseObject(json);
                byte[] bytes = fileService.download(jsonObject.getString("downloadKey"));
                if(null != bytes){
                    ZipInfoUtil.exportZipByte(response, jsonObject.getString("fileName"), bytes, this.getCurUserId());
                }
            }
        }catch (Exception e) {
            setError(e.getMessage());
            log.error(e.getMessage());
        }
    }

    @RequestMapping(value = "portalDownload",method = RequestMethod.POST)
    @ApiOperation(value="首页文件下载",notes="下载文件")
    public void portalDownload(@RequestBody String json, HttpServletResponse response) {
        log.info("首页下载文件{}开始",json);
        String path = null;
        if(System.getProperty("os.name").toLowerCase().indexOf("windows")>-1){
            path = winPath;
        }else{
            path = unixPath;
        }
        try {
            if(StringUtils.isNotBlank(json)){
                JSONObject jsonObject =  JSONObject.parseObject(json);
                String filename = jsonObject.getString("filename");
                File file = new File(path+filename);
                if(file.exists()){
                    ResponseExportUtil.exportFile(response, file);
                }
                log.info("首页下载文件{}结束",filename);
            }
        }catch (Exception e) {
            log.error("首页下载文件异常"+e);
        }
    }
}
